package org.jbpm.pvm.internal.email.impl;

import org.jbpm.api.JbpmException;
import org.jbpm.pvm.internal.email.spi.MailSession;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

@Service
public class MailSessionImpl implements MailSession {

    private List<MailServer> mailServers;

    @Override
    public void send(Collection<Message> emails) {
        //自定义代码，加载配置文件并读取信息
        InputStream is = this.getClass().getResourceAsStream("/jbpm.mail.properties");
        Properties props = new Properties();
        String user = null;
        String password = null;
        String host = null;
        try {
            props.load(is);
            host = props.getProperty("mail.smtp.host");
            user = props.getProperty("mail.from");
            password = props.getProperty("mail.smtp.password");
        } catch (IOException e1) {
            e1.printStackTrace();
        }


        // Emails need to have the sessions populated.
        for (Message email : emails) {
            try {
                Address[] to = email.getRecipients(Message.RecipientType.TO);
                Address[] cc = email.getRecipients(Message.RecipientType.CC);
                Address[] bcc = email.getRecipients(Message.RecipientType.BCC);

                for (MailServer mailServer : mailServers) {
                    // Need to apply filter.
                    AddressFilter addressFilter = mailServer.getAddressFilter();
                    if (addressFilter != null) {
                        // Set the email with the new filtered addresses.
                        email.setRecipients(Message.RecipientType.TO, addressFilter.filter(to));
                        email.setRecipients(Message.RecipientType.CC, addressFilter.filter(cc));
                        email.setRecipients(Message.RecipientType.BCC, addressFilter.filter(bcc));
                    }

                    // if sender is not present, use local address
                    Session mailSession = mailServer.getMailSession();
                    if (email.getFrom() == null) {
                        email.setFrom(InternetAddress.getLocalAddress(mailSession));
                    }

                    // If there is someone to send it to, then send it.
                    Address[] recipients = email.getAllRecipients();
                    if (recipients.length > 0) {
                        Transport transport = mailSession.getTransport(recipients[0]);
                        try {
                            transport.connect(host, user, password);
                            transport.sendMessage(email, recipients);
                        } finally {
                            transport.close();
                        }
                    }
                }
            } catch (MessagingException e) {
                throw new JbpmException("could not send email: " + email, e);
            }
        }
    }

    public List<MailServer> getMailServers() {
        return mailServers;
    }

    protected void setMailServers(List<MailServer> mailServers) {
        this.mailServers = mailServers;
    }

}